﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using System;
using Rio.Doc.Business.Repository.Translator;
using Rio.Doc.DataAccess;

#endregion

namespace Rio.Doc.Business.Repository
{
    public class AlertRepository : AlertDer
    {
        /// <summary>
        /// Returns all Alert of the day
        /// </summary>
        /// <returns>The list of all Alerts of the day </returns>
        public static List<Alert> GetAllAlert()
        {
            var l_lstAlerts = new List<Alert>();
            var l_lstAlertsReturn = new List<Alert>();
            l_lstAlerts = AlertTranslator.TranslateAlert(DA_Alert.GetAllAlert());

            foreach (Alert l_oAlert in l_lstAlerts)
            {
                var l_oTempAlert = l_oAlert;
                l_oTempAlert.GroupList = GeneralGroupTranslator.TranslateGroupWithRights(DA_Alert.GetAllGroupsOfAnAlert(l_oAlert.Identifier));
                l_lstAlertsReturn.Add(l_oTempAlert);
            }


            return l_lstAlertsReturn;
        }

        /// <summary>
        /// Get all alerts of a folder
        /// </summary>
        /// <param name="p_lFolderTypeId">The identifier of the foldertpe</param>
        /// <param name="p_lFolderId">The identifier of the folder</param>
        /// <returns> The database alerts that ocurs today for a folder</returns>
        public static List<Alert> GetAllAlertOfAFolder(long p_lFolderTypeId, long p_lFolderId)
        {
            var l_lstAlerts = new List<Alert>();
            var l_lstAlertsReturn = new List<Alert>();
            try
            {
                l_lstAlerts = AlertTranslator.TranslateAlert(DA_Alert.GetAllAlertOfAFolder(p_lFolderTypeId, p_lFolderId));
                foreach (var l_oAlert in l_lstAlerts)
                {
                    var l_oTempAlert = l_oAlert;
                    l_oTempAlert.GroupList = GeneralGroupTranslator.TranslateGroupWithRights(DA_Alert.GetAllGroupsOfAnAlert(l_oAlert.Identifier));
                    l_lstAlertsReturn.Add(l_oTempAlert);
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(ex.Message);
            }

            return l_lstAlertsReturn;
        }

        /// <summary>
        /// Get all folders from an alert
        /// </summary>
        /// <param name="p_lAlertId">Identifier from an alert</param>
        /// <returns>the folders associated with the alert</returns>
        public static List<Folder> GetAllFolderOfAnAlert(long p_lAlertId)
        {
            var l_lstFolders = new List<Folder>();

            try
            {
                // Retreives the folder of an alert
                l_lstFolders = FolderTranslator.TranslateFolderWithoutIndex(DA_Alert.GetAllFodlerOfAnAlert(p_lAlertId));

            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(ex.Message);
            }

            return l_lstFolders;
        }

        /// <summary>
        /// Add an alert
        /// </summary>
        /// <param name="p_sAlerteName">the name ofthe alert</param>
        /// <param name="p_sAlerteObject">The object to include in the alert</param>
        /// <param name="p_sAlerteBody">the body of the alert</param>
        /// <param name="p_dAlerteDate">the date when the alert must be sent</param>
        /// <param name="p_lUserId">the user identifier tahtat creates the alert</param>
        /// <param name="p_dicFolderTypeIdFolderId">The p_dic folder type id folder id.</param>
        /// <param name="p_lstGroupIds">The P_LST group ids.</param>
        /// <param name="action">The action.</param>
        /// <returns>
        /// The database identifier of the new alert
        /// </returns>
        public static ReturnLong AddAlert(string p_sAlerteName, string p_sAlerteObject, string p_sAlerteBody, DateTime p_dAlerteDate, long p_lUserId, Dictionary<long, long> p_dicFolderTypeIdFolderId, List<long> p_lstGroupIds, int? action)
        {
            var l_oRet = new ReturnLong();

            try
            {
                // Retreives The alert Id newly created
                l_oRet.Value = DA_Alert.AddAlert(p_sAlerteName, p_sAlerteObject, p_sAlerteBody, p_dAlerteDate, p_lUserId, action);

                //if (l_oRet.Value != null)
                //{
                    AddAlertForAFolder(l_oRet.Value, p_dicFolderTypeIdFolderId);
                    AddAlertForAGroup(p_lstGroupIds, l_oRet.Value);
                //}
            }
            catch (Exception ex)
            {
                l_oRet = new ReturnLong("AddFolder(p_sAlerteName<" + p_sAlerteName + "> p_sAlerteObject<" + p_sAlerteObject + "> p_sAlerteBody<" + p_sAlerteBody + "> p_dAlerteDate<" + p_dAlerteDate + "> p_lUserId<" + p_lUserId + ">)", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oRet.FullErrorMessage);
            }
            return l_oRet;
        }

        /// <summary>
        /// Add an alert for a user
        /// </summary>
        /// <param name="p_lstGroupIds">The list of all group identifiers</param>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        public static void AddAlertForAGroup(List<long> p_lstGroupIds, long p_lAlertId)
        {

            try
            {
                foreach (var l_lOneGroupId in p_lstGroupIds)
                {
                    DA_Alert.AddAlertForAGroup(l_lOneGroupId, p_lAlertId);
                }
                
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("AddFolder(p_lstGroupIds.Count<" + p_lstGroupIds.Count + "> p_lAlertId<" + p_lAlertId + ">) exception " + ex.Message);
            }
        }

        /// <summary>
        /// Add an alert for a Folder
        /// </summary>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        /// <param name="p_dicFolderIdFolderTypeId">A dictionnary containing foreach folder identifier, the identifier of its foldertype</param>
        public static void AddAlertForAFolder(long p_lAlertId, Dictionary<long, long> p_dicFolderIdFolderTypeId)
        {
            try
            {
                foreach (var l_lFolderId in p_dicFolderIdFolderTypeId.Keys)
                {
                    DA_Alert.AddAlertForAFolder(p_lAlertId, p_dicFolderIdFolderTypeId[l_lFolderId], l_lFolderId);
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("AddFolder(p_lAlertId<" + p_lAlertId + "> p_dicFolderIdFolderTypeId.Count<" + p_dicFolderIdFolderTypeId.Count + ">) exception " + ex.Message);
            }
        }

        /// <summary>
        /// Update an alert
        /// </summary>
        /// <param name="p_dicAlertIdStatusId">A dictionnary containing foreach alert identifier, the new status</param>
        /// <param name="p_lUserId">The identifier of the user</param>
        public static void UpdateAlert(Dictionary<long, long> p_dicAlertIdStatusId, long p_lUserId)
        {
            try
            {
                foreach (var l_lAlertId in p_dicAlertIdStatusId.Keys)
                {
                    DA_Alert.UpdateAlert(l_lAlertId, p_lUserId, p_dicAlertIdStatusId[l_lAlertId]);
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("AddFolder(p_dicAlertIdStatusId.Count<" + p_dicAlertIdStatusId.Count + "> p_lUserId<" + p_lUserId + ">) exception " + ex.Message);
            }
        }
    }
}
